package cn.hls.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import cn.hls.dao.CertSetDao;
import cn.hls.entity.CertSet;
import cn.hls.entity.vo.PageInfo;
/**
 *@fileName: CertSetDaoImpl
 *@package: cn.christ.dao.impl
 *@Description: 证书到期提醒数据操作接口实现类
 *@author: yinghui.zhang
 *@email: uuhui@163.com
 *@date: 2015-09-25
 */
@Component("certSetDao")
public class CertSetDaoImpl extends BaseDaoImpl<CertSet> implements CertSetDao {
	@Override
	public boolean exists(CertSet entity)throws Exception{
		StringBuilder hql=new StringBuilder("select count(id) from CertSet p where deleted=? ");
		List<Object>params=new ArrayList<Object>();
		params.add(false);
		if(entity!=null){
			//按编号查询
			if(entity.getId()!=null){
				hql.append("and p.id<>? ");
				params.add(entity.getId());
			}
			//按证书代码查询
			if(StringUtils.hasText(entity.getCertCode())){
				hql.append("and p.certCode=? ");
				params.add(entity.getCertCode().trim());
			}
			//按证书名称查询
			if(StringUtils.hasText(entity.getCertName())){
				hql.append("and p.certName=? ");
				params.add(entity.getCertName().trim());
			}
		}
		Long result=countByHql(hql.toString(), params.toArray());
		return result>0?true:false;
	}

	@Override
	public PageInfo<CertSet> loadCertSetInfo(PageInfo<CertSet> page,
			CertSet entity) throws Exception {
		StringBuilder selectHql=new StringBuilder("from CertSet t where t.deleted=? ");
		StringBuilder countHql=new StringBuilder("select count(id) ");
		List<Object> params=new ArrayList<Object>();
		params.add(false);
		if(entity!=null){
			//按名称查询
			if(StringUtils.hasText(entity.getCertName())){
				selectHql.append("and t.certName like ? ");
				params.add("%"+entity.getCertName().trim()+"%");
			}
			//按代码查询
			if(StringUtils.hasText(entity.getCertCode())){
				selectHql.append("and t.certCode like ? ");
				params.add("%"+entity.getCertCode().trim()+"%");
			}
		}
		countHql.append(selectHql);
		return find(selectHql.toString(), countHql.toString(), params.toArray(), page);
	}
 

	@Override
	public List<CertSet> loadCertSetInfo(CertSet entity) throws Exception {
		StringBuilder selectHql=new StringBuilder("from CertSet t where t.deleted=? ");
		List<Object> params=new ArrayList<Object>();
		params.add(false);
		if(entity!=null){
			//按名称查询
			if(StringUtils.hasText(entity.getCertName())){
				selectHql.append("and t.certName like ? ");
				params.add("%"+entity.getCertName().trim()+"%");
			}
			//按代码查询
			if(StringUtils.hasText(entity.getCertCode())){
				selectHql.append("and t.certCode like ? ");
				params.add("%"+entity.getCertCode().trim()+"%");
			}
			 
		}
		return findByHql(selectHql.toString(), params.toArray());
	}
	  public Map<String,CertSet> loadcertSetInfoToMap(CertSet entity)throws Exception{
		  List<CertSet>list=loadCertSetInfo(entity);
		  Map<String,CertSet>map=new HashMap<String, CertSet>();
		  for(CertSet item:list){
			  map.put(item.getCertCode(),item);
		  }
		  return map;
		  
	  }
}
